from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.chrome import webdriver
from selenium.webdriver.ie.webdriver import WebDriver
from selenium.webdriver.support.wait import WebDriverWait
from pages.basePage import BasePage


# driver = webdriver.WebDriver()
# driver.get('http://192.168.195.145/user.php')
# driver.maximize_window()
# time.sleep(2)

class LoginPage(BasePage):
    #属性
    euser=(By.XPATH,'//tbody/tr[1]/td[2]/input')
    epwd=(By.XPATH,"//input[@name='password']")
    ebut=(By.XPATH,"//input[@name='submit']")
    elogin_assert=(By.CSS_SELECTOR,"[id='ECS_MEMBERZONE']")


    # def assert_login(self):
    #     try:
    #
    #         # 尝试定位弹窗
    #         alert = self.driver.switch_to.alert
    #         result = '错误：' + alert.text
    #         return result
    #     except Exception:
    #         elogmin = self.findEle(self.elogin_assert)  # 登录成功显示你好，admin | 退出 |
    #         if '你好' in elogmin.text:
    #             return "登录成功"
    #         # 欢迎光临本店，登录 | 注册 |
    #         elif ' 欢迎光临本店，登录 | 注册 |' in elogmin.text:
    #             return "错误：登录失败，用户名密码错误"

    #行为
    def login(self,euser="hjy",epwd="1234567A"):
        self.driver.implicitly_wait(3)
        #输入用户名
        # self.driver.find_element(*self.euser).send_keys(euser)
        # self.findEle(self.euser).send_keys(euser)
        name = self.findEle(self.euser)
        self.inputText(name,euser)
        #输入密码
        # self.driver.find_element(*self.epwd).send_keys(epwd)
        # self.findEle(self.epwd).send_keys(epwd)
        mima = self.findEle(self.epwd)
        self.inputText1(mima,epwd)
        #点击登录
        # self.driver.find_element(*self.ebut).click()
        # self.findEle(self.ebut).click()
        dian = self.findEle(self.ebut)
        self.inputText2(dian)
        # return self.assert_login()




    # def findEle(self,locator):
    #     try:
    #         print('开始查找元素')
    #         ele=WebDriverWait(self.driver,10,1).until(lambda d:d.find_element(*locator))
    #     except Exception as e:
    #         print(f"找到元素{locator},异常信息{e}")
    #     else:
    #         print(f'找到元素{locator}')
    #         return ele
    #     finally:
    #         print(f'查找完成{locator}')
    #
    # def inputText(self,ele,msg):
    #     try:
    #         print('向元素中输入内容')
    #         ele.send_keys(msg)
    #     except Exception as e:
    #         print(f'查找元素发生异常{ele},异常信息{e}')
    #     else:
    #         print(f'元素找到了{ele}')
    #     finally:
    #         print(f'查找完成')



if __name__ == '__main__':
    lg=LoginPage()
    #正确的
    # lg.login()
    #错误的
    print(lg.login('hjy','1234567A'))
    # '1234567A'
